package com.empire.module.member.service.presentationpersonrelation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.empire.module.member.dal.dataobject.casepersonrelation.CasePersonRelationDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.presentationpersonrelation.vo.*;
import com.empire.module.member.dal.dataobject.presentationpersonrelation.PresentationPersonRelationDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.presentationpersonrelation.PresentationPersonRelationMapper;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 报告-人员关系 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class PresentationPersonRelationServiceImpl extends ServiceImpl<PresentationPersonRelationMapper, PresentationPersonRelationDO> implements PresentationPersonRelationService {

    @Resource
    private PresentationPersonRelationMapper presentationPersonRelationMapper;

    @Override
    public Integer createPresentationPersonRelation(PresentationPersonRelationSaveReqVO createReqVO) {
        // 插入
        List<Long> personIdList = createReqVO.getPersonIdList();
        List<PresentationPersonRelationDO> relationList = new ArrayList<>();
        for (Long personId : personIdList) {
            PresentationPersonRelationDO relation = new PresentationPersonRelationDO();
            relation.setPersonId(personId);
            relation.setPresentationId(createReqVO.getPresentationId());
            relation.setCreateTime(LocalDateTime.now());
            relationList.add(relation);
        }
        presentationPersonRelationMapper.insertBatch(relationList);
        // 返回
        return relationList.size();
    }

    @Override
    public void deletePresentationPersonRelation(Long id, Long personId) {
        // 删除
        LambdaQueryWrapper<PresentationPersonRelationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PresentationPersonRelationDO::getPresentationId, id);
        queryWrapper.eq(PresentationPersonRelationDO::getPersonId, personId);
        presentationPersonRelationMapper.delete(queryWrapper);
    }

    @Override
    public PageResult<PresentationPersonRelationDO> getPresentationPersonRelationPage(PresentationPersonRelationPageReqVO pageReqVO) {
        return presentationPersonRelationMapper.selectPage(pageReqVO);
    }

    @Override
    public Set<Long> getPresentationPersonIds(Long presentationId) {
        LambdaQueryWrapper<PresentationPersonRelationDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PresentationPersonRelationDO::getPresentationId, presentationId);
        List<PresentationPersonRelationDO> relationDOList = presentationPersonRelationMapper.selectList(wrapper);
        return relationDOList.stream().map(PresentationPersonRelationDO::getPersonId).collect(Collectors.toSet());
    }

}